package com.dayaokeji.rhythmschoolstudent.client.common.resource;


import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dayaokeji.rhythmschoolstudent.R;
import com.dayaokeji.rhythmschoolstudent.client.common.base.common.BaseActivity;
import com.dayaokeji.rhythmschoolstudent.utils.FileUtil;
import com.dayaokeji.rhythmschoolstudent.utils.ObjectUtils;
import com.dayaokeji.rhythmschoolstudent.utils.PermissionUtil;
import com.dayaokeji.rhythmschoolstudent.utils.ServerCallback;
import com.dayaokeji.rhythmschoolstudent.utils.ToastUtils;
import com.dayaokeji.rhythmschoolstudent.utils.UserUtils;
import com.dayaokeji.server_api.ApiUtils;
import com.dayaokeji.server_api.ServerResponse;
import com.dayaokeji.server_api.api.ResourceApi;
import com.dayaokeji.server_api.domain.FileInfo;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.List;

import butterknife.BindView;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Consumer;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 资源共享
 */
public class SharedResourceActivity extends BaseActivity {

    public static final String EXTRA_REL_ID = "rel_id";
    public static final String EXTRA_REL_TYPE = "rel_type";
    public static final String EXTRA_TEACHER_ID = "teacher_id";
    public static final String EXTRA_RESOURSE_TYPE = "resourse_type";


    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rv_resource_list)
    RecyclerView rvResourceList;

    private int relId;

    private static final ResourceApi resourceApi = ApiUtils.getApi(ResourceApi.class);
    private int relType;
    private int teacherId;
    private int resourseType;

    private SharedResourceAdapter sharedResourceAdapter;
    private Call<ServerResponse<List<FileInfo>>> listCall;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shared_resource);
        setSupportActionBar(toolbar);
        relId = getIntent().getIntExtra(EXTRA_REL_ID, -1);
        relType = getIntent().getIntExtra(EXTRA_REL_TYPE, -1);
        teacherId = getIntent().getIntExtra(EXTRA_TEACHER_ID, -1);
        resourseType = getIntent().getIntExtra(EXTRA_RESOURSE_TYPE,-1);
        if (relId == -1 || teacherId == -1) {
            ToastUtils.warning(R.string.data_incorrect);
            return;
        }
        init();
    }

    private void init() {
        setRecyclerView();
        initData();
        setOnListener();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (ObjectUtils.equals(UserUtils.getUserInfo().getId(),String.valueOf(teacherId))) {
            getMenuInflater().inflate(R.menu.resource_shared,menu);
        }
        return super.onCreateOptionsMenu(menu);
    }

    private void setRecyclerView() {
        rvResourceList.setLayoutManager(new LinearLayoutManager(this));
        sharedResourceAdapter = new SharedResourceAdapter();
        rvResourceList.setAdapter(sharedResourceAdapter);
        sharedResourceAdapter.setEmptyView(R.layout.empty_layout,rvResourceList);
    }

    private void setOnListener() {
        sharedResourceAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                FileInfo fileInfo = (FileInfo) baseQuickAdapter.getData().get(i);
                ShareResourceDetailActivity.Companion.navigation(SharedResourceActivity.this, fileInfo);
            }
        });
    }

    private void initData() {
        listCall = resourceApi.select(relId, relType, resourseType);
        listCall.enqueue(new ServerCallback<List<FileInfo>>() {
            @Override
            public void onResponse(boolean success, ServerResponse<List<FileInfo>> response) {
                if (success) {
                    sharedResourceAdapter.setNewData(response.getBody());
                }
            }
        });
    }



    @Override
    protected void onDestroy() {

        if (listCall != null) {
            listCall.cancel();
        }
        super.onDestroy();
    }
}
